package me.maxwin.view;

import java.io.UnsupportedEncodingException;

import com.example.lightble.R;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DevNameDialog extends AlertDialog  implements OnClickListener{

	private EditText mEditText;
	private LinearLayout mAlarmHint; //警告提示.
	private TextView tvShowName;
	private String retName=null;
	public DevNameDialog(Context context,String  title,int id) {
		super(context);
		// TODO Auto-generated constructor stub
		
		  LayoutInflater inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	      View view = inflater.inflate(R.layout.name_edittext, null);
	      mEditText=(EditText) view.findViewById(R.id.id_name_edittext_edit);  
	       mAlarmHint=(LinearLayout)view.findViewById(R.id.id_name_edittext_hint_layout);
	       tvShowName=(TextView)view.findViewById(R.id.id_name_edittext_showname);
	       mAlarmHint.setVisibility(View.GONE);
	       
	      mEditText.addTextChangedListener(mInputTextWatcher);
	      setTitle(title);
	      retId=id;
		setView(view);
		setButton("设定", this);
		setButton2("取消", (OnClickListener) null);
		
	}
	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		if(mOKListener!=null)
			mOKListener.onOk(this, retId,retName);
	}
	
	public interface onOKListener {	    
        void onOk(DevNameDialog v,int id,String str);
    }
	private onOKListener mOKListener=null;
	private int retId;
	
	public void setOnOKListener(onOKListener listener)
	{
		mOKListener=listener;
	}
	 private TextWatcher mInputTextWatcher = new TextWatcher() {
	        private String temp;

	        @Override
	        public void onTextChanged(CharSequence s, int start, int before, int count) {
	            temp = s.toString();
	        	Log.i("sno","----onTextChanged--------------");
	        }

	        @Override
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	        }

	        @Override
	        public void afterTextChanged(Editable s) {
	            if (!TextUtils.isEmpty(temp)) {
	                String limitSubstring = getLimitSubstring(temp);
	                if (!TextUtils.isEmpty(limitSubstring)) 
	                {
	                    if (!limitSubstring.equals(temp)) {
	                    	Log.i("sno","字数已超过限制,limitSubstring:"+limitSubstring);
	                    	mAlarmHint.setVisibility(View.VISIBLE);
	                    	tvShowName.setText(limitSubstring);
	                    }else
	                    	mAlarmHint.setVisibility(View.GONE);
	                }
	                retName=limitSubstring;
	            }
	        }
	    };

	    private String getLimitSubstring(String inputStr) {
	        int orignLen = inputStr.length();
	        int resultLen = 0;
	        String temp = null;
	        for (int i = 0; i < orignLen; i++) 
	        {
	            temp = inputStr.substring(i, i + 1);
	            try 
	            {  // 3 bytes to indicate chinese word,1 byte to indicate english word ,in utf-8 encode
	                if (temp.getBytes("utf-8").length == 3) {
	                    resultLen += 2;
	                } else {
	                    resultLen++;
	                }
	            } catch (UnsupportedEncodingException e) {
	                e.printStackTrace();
	            }
	            
	            if (resultLen > 12) {  //最大12个字符
	                return inputStr.substring(0, i);
	            }
	        }
	        return inputStr;
	    }
}
